package main

import (
	"fmt"

	"gitee.com/c_z/jlcdata"
	"github.com/cz-theng/czkit-go/log"
	"github.com/spf13/cobra"
	"go.uber.org/zap/zapcore"
)

var (
	_version    bool
	_configFile string
	_page       int32
)

var rootCMD = &cobra.Command{
	Use:   "jlc",
	Short: "start an jlc",
	Long:  `start an jlc`,
	Run:   _main,
}

func init() {
	rootCMD.PersistentFlags().BoolVarP(&_version, "version", "v", false, "print version of jlc")
	//rootCMD.PersistentFlags().StringVarP(&_configFile, "config", "c", "", "config file path for jlc")
	rootCMD.PersistentFlags().Int32VarP(&_page, "page", "p", 0, "page of capacitance for jlc")
}

func main() {
	rootCMD.Execute()
}

func dumpVersion() {
	fmt.Printf("%s\n", "0.1.0")
}

func _main(cmd *cobra.Command, args []string) {
	if _version {
		dumpVersion()
		return
	}
	// if len(_configFile) > 0 {
	// 	if err := loadConfig(_configFile); err != nil {
	// 		return
	// 	}
	// } else {
	// 	cmd.Usage()
	// 	return
	// }

	// logFile := "jlc.log"
	// logPath := "./"
	// if len(config.LogPath) > 0 {
	// 	logPath = config.LogPath
	// }
	// if len(config.LogFile) > 0 {
	// 	logFile = config.LogFile
	// }

	// logNameOpt := log.WithLogName(logFile)
	// logPathOpt := log.WithLogPath(logPath)
	logPathOpt := log.WithLogLevel(zapcore.ErrorLevel)
	log.Init(logPathOpt)

	c, err := jlcdata.QueryCapacitanceCount()
	if err != nil {
		fmt.Printf("Error:%s \n", err.Error())
		return
	}
	fmt.Printf("Capacitance Count:%d\n", c)
	if _page > 0 {
		p, err := jlcdata.QueryCapacitance(_page)
		if err != nil {
			fmt.Printf("Error:%s \n", err.Error())
			return
		}
		fmt.Printf("Page %d has %d products \n", _page, len(p.ProductRecordList))
		fmt.Printf("And the first one is %v \n", p.ProductRecordList[0])
	}
}
